package com.hyacinth.springmvcdemo.controller;

import com.hyacinth.springmvcdemo.entity.Person;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * @Author: Yun
 * @Date: 2025/6/11
 */
@RestController
@RequestMapping("/param")
public class ParamController {
    @RequestMapping("/m1")
    public String method1(String name) {
        return "name: " + name;
    }

    @RequestMapping("/m4")
    public String method4(@RequestParam("name") String username) {
        return "username: " + username;
    }

    @RequestMapping("/m2")
    public String method2(String name, String password) {
        return "name: " + name + ", password: " + password;
    }

    @RequestMapping("/m3")
    public String method3(Person person) {
        return "person: " + person.toString();
    }


    //  数组参数：请求参数名与形参数组名称相同且请求参数为多个, 后端定义数组类型形参即可接收参数
    //  http://127.0.0.1:8080/param/m5?arrayParam=zhangsan&arrayParam=lisi&arrayParam=wangwu
    //  或者使⽤ http://127.0.0.1:8080/param/m6?listParam=zhangsan%2clisi%2cwangwu
    //  或者使⽤ http://127.0.0.1:8080/param/m5?arrayParam=zhangsan,lisi,wangwu
    @RequestMapping("/m5")
    public String method5(String[] arrayParam) {
        return Arrays.toString(arrayParam);
    }

    // ⽅式⼀: http://127.0.0.1:8080/param/m6?listParam=zhangsan&listParam=lisi&listParam=wangwu
    // ⽅式⼆: http://127.0.0.1:8080/param/m6?listParam=zhangsan%2clisi%2cwangwu
    // %2c 是逗号的转义编码, 解码后的url为: http://127.0.0.1:8080/param/m6?listParam=zhangsan,lisi,wangwu
    @RequestMapping("/m6")
    public String method6(@RequestParam List<String> listParam) {
        return "size:" + listParam.size() + ",listParam:" + listParam;
    }

    @RequestMapping("/m7")
    public Object method7(@RequestBody Person person) {
        return person.toString();
    }

    @RequestMapping("/m8/{id}/{name}")
    public String method8(@PathVariable Integer id, @PathVariable("name") String userName) {
        return "id: " + id + ", name: " + userName;
    }

    @RequestMapping("/m9")
    public String getfile(@RequestPart("file") MultipartFile file) throws IOException {
        //获取⽂件名称
        String fileName = file.getOriginalFilename();
        //⽂件上传到指定路径
        file.transferTo(new File("D:/temp/" + file.getOriginalFilename()));
        return "接收到⽂件名称为: " + fileName;
    }

    @RequestMapping("/m10")
    public String method10(HttpServletRequest request, HttpServletResponse response) {
        // 获取所有 cookie 信息
        jakarta.servlet.http.Cookie[] cookies = request.getCookies();
        //打印Cookie信息
        StringBuilder builder = new StringBuilder();
        if (cookies != null) {
            for (Cookie ck : cookies) {
                builder.append(ck.getName() + ":" + ck.getValue());
            }
        }
        return "Cookie信息:" + builder;
    }

    @RequestMapping("/getCookie")
    public String cookie(@CookieValue("bite") String bite) {
        return "bite：" + bite;
    }

    @RequestMapping("/setSession")
    public String setsess(HttpServletRequest request) {
        // 获取Session对象
        HttpSession session = request.getSession();
        if (session != null) {
            session.setAttribute("username", "java");
        }
        return "session 存储成功";
    }

    @RequestMapping("/getSession")
    public String sess(HttpServletRequest request) {
        // 如果 session 不存在, 不会⾃动创建
        HttpSession session = request.getSession(false);
        String username = null;
        if (session != null && session.getAttribute("username") != null) {
            username = (String) session.getAttribute("username");
        }
        return "username：" + username;
    }

    @RequestMapping("/getSession2")
    public String sess2(@SessionAttribute(value = "username", required = false)
                        String username) {
        return "username：" + username;
    }

    @RequestMapping("/getSession3")
    public String sess3(HttpSession session) {
        String username = (String) session.getAttribute("username");
        return "username：" + username;
    }

    @RequestMapping("/param10")
    public String param10(HttpServletRequest request, HttpServletResponse response) {
        String userAgent = request.getHeader("User-Agent");
        return "name：" + userAgent;
    }
}
